Ext.namespace("Dudley");
Ext.namespace("Dudley.lims");

Dudley.lims.sequencingmgr = function() {

    var loaded = false;
    var seqPanel = {};
    Ext.History.init();
    var tokenDelimiter = ':';

    // Handle this change event in order to restore the UI to the appropriate history state
    /*Ext.History.on('change', function(token) {
     if (token) {
     var parts = token.split(tokenDelimiter);
     var tabPanel = Ext.getCmp(parts[0]);
     var tabId = parts[1];

     tabPanel.show();
     tabPanel.setActiveTab(tabId);
     } else {
     // This is the initial default state.  Necessary if you navigate starting from the
     // page without any existing history token params and go back to the start state.
     seqPanel.setActiveTab(0);
     seqPanel.getItem(0).setActiveTab(0);
     }
     });*/

    // public
    return {

        loadSeqSearchResult: function() {
            Ext.getDom("sample-search-result-list").innerHTML = "";
        },

        getSeqLoaded: function() {
            return loaded;
        },

        setSeqLoaded: function(l) {
            loaded = l;
        },

        getOmicPanel: function() {
            return seqPanel;
        },

        clearPane: function() {
            Ext.getDom("yseq-search-results").innerHTML = "";
            Ext.getDom("wo-form").innerHTML = "";
        },

        loadSequencingManagement: function(callback) {
            var win = new Ext.Window({
                title: 'Sequencing Loading...',
                width: 400,
                minWidth: 350,
                height: 150,
                modal: true,
                closeAction: 'hide',
                bodyStyle: 'padding:10px;',
                html: "Initializing Dudley Sequencing...",
                bbar: new Ext.ux.StatusBar({
                    id: 'init-yo-statusbar',
                    defaultText: 'Ready'
                })
            });

            Dudley.lims.YSEQCGS.initCGS(function () {
                Ext.Msg.alert("warning", "You do not have permission to view any Crossings");
                win.close();
            });
            win.on("show", function () {
                var sb = Ext.getCmp("init-yo-statusbar");
                sb.showBusy();
            });
            if (!loaded) {
                Dudley.lims.YSEQEntry.initEntry(function() {
                    loaded = true;
                    seqPanel = new Ext.TabPanel({
                        autoHeight: true,
                        activeTab: 0,
                        border: true,
                        //autoScroll: true,
                        renderTo: "content-sequencing",
                        items: [ Dudley.lims.YSEQEntry.getEntryPanel(), Dudley.lims.YSEQCGS.getCGSPanel(true)/*, Dudley.lims.YSEQRadtag.getRadtagPanel()*/],
                        listeners: {
                            'tabchange': function(tabPanel, tab) {
                                Ext.History.add(tabPanel.id + tokenDelimiter + tab.id);
                                win.close();
                            }
                        }
                    });
                    callback();
                });
            }
        }
    };
}();
